package com.wtc.service;

import com.wtc.entity.Student;
import com.wtc.utils.MybatisUtility;
import com.wtc.utils.Utility;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class StudentService {
    public void select() throws IOException {
        SqlSession sqlSession = MybatisUtility.getSqlSession();
        List<Student> studentList = sqlSession.selectList("com.wtc.dao.StudentDao.selectAllStudents");
        for (Student stu : studentList) {
            System.out.println(stu);
        }
        sqlSession.close();
    }

    public void insert() throws IOException {
        SqlSession sqlSession = MybatisUtility.getSqlSession();
        System.out.print("请输入ID：");
        Integer id = Utility.readInt();
        System.out.print("请输入姓名：");
        String name = Utility.readString(10);
        System.out.print("请输入年龄：");
        Integer age = Utility.readInt();
        System.out.print("请输入邮箱：");
        String email = Utility.readString(20);
        Student student = new Student(id, name, age, email);
        //6.执行插入 insert
        int rows = sqlSession.insert("com.wtc.dao.StudentDao.add", student);
        //7.提交事务
        sqlSession.commit();
        // 8.输出结果
        if (rows == 1) {
            System.out.println("添加成功！");
        } else {
            System.out.println("添加失败！");
        }
        //9.关闭 SqlSession对象连接
        sqlSession.close();
    }

    public void update() throws IOException {
        SqlSession sqlSession = MybatisUtility.getSqlSession();
        List<Student> studentList = sqlSession.selectList("com.wtc.dao.StudentDao.selectAllStudents");
        System.out.println("以下是可修改信息：");
        for (Student stu : studentList) {
            System.out.println(stu);
        }
        System.out.print("请输入要修改信息的ID：");
        Integer id = Utility.readInt();
        System.out.print("请输入修改后的姓名：");
        String name = Utility.readString(10);
        System.out.print("请输入修改后的年龄：");
        Integer age = Utility.readInt();
        System.out.print("请输入修改后的邮箱：");
        String email = Utility.readString(20);
        //5.创建保存数据的对象
        Student student = new Student(id, name, age, email);
        //6.执行更新 update
        int rows = sqlSession.update("com.wtc.dao.StudentDao.update", student);
        //7.提交事务
        sqlSession.commit();
        if (rows == 1) {
            System.out.println("修改成功！");
        } else {
            System.out.println("修改失败！");
        }
        //8.关闭 SqlSession
        sqlSession.close();
    }

    public void delete() throws IOException {
        SqlSession sqlSession = MybatisUtility.getSqlSession();
        List<Student> studentList = sqlSession.selectList("com.wtc.dao.StudentDao.selectAllStudents");
        System.out.println("以下是可删除信息：");
        for (Student stu : studentList) {
            System.out.println(stu);
        }
        System.out.print("请输入你想要删除信息的ID：");
        Integer id = Utility.readInt();
        //6.执行删除 delete
        int rows = sqlSession.delete("com.wtc.dao.StudentDao.delete", id);
        //7.提交事务
        sqlSession.commit();
        if (rows == 1) {
            System.out.println("删除成功！");
        } else {
            System.out.println("删除失败！");
        }
        sqlSession.close();
    }
}
